//    PAM Quiz
//    This is an executable to test the questions for the PAM module.
//    Copyright (C) 2009 Ryan Latta
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <stdio.h>

struct Question {
  char question[9];
  char answer[4];
};

void makeMathQuestion(struct Question *);

void makeMathQuestion(struct Question *q) {
  const char *operands = "+-*/";
  srand(time(NULL));
  int number1, number2, operand;
  
  operand = rand() % 3;
  number1 = rand() % (10 - 1) + 1;
  number2 = rand() % (10 - 1) + 1;
  //printf("Random Numbers generated...\n");
  
  sprintf(q->question, "%d %c %d = ", number1, operands[operand], number2);
  
  //printf("Question assigned...\n");
  
  switch (operand) {
    case 0:
      sprintf(q->answer, "%d", number1 + number2);
      break;
    case 1:
      sprintf(q->answer, "%d", number1 - number2);
      break;
    case 2:
      sprintf(q->answer, "%d", number1 * number2);
      break;
    case 3: 
      sprintf(q->answer, "%f.2", (float)number1 / number2);
      break;
    default:
      operand = 2;
  }
  //printf("Answer assigned...\n");
}

int main() {
  struct Question q;
  int i;
  char answer[4];
  
  printf("Please answer the following...\n");
  
  for(i = 0; i < 5; i++) {
    makeMathQuestion(&q);
    printf("%s", q.question);
    scanf("%c", answer);
    
    if(strcmp(answer, q.answer) == 0) {
      printf("Correct!\n");
    }
    else {
      printf("Incorrect\n");
    }    
    memset(q.question, '\0', 9);
    memset(q.answer, '\0', 4);
    memset(answer, '\0', 4);

    
  }    
  return 0;
}